﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using OznerOA.Common;
using OznerOA.BLL;
using OznerOA.Model;

namespace OznerOA.UI
{
    public partial class FrmLogin : FrmBase
    {
        private UserBLL userBLL = UserBLL.GetInstance();
        //配置文件的自动登录是否需要生效
        private bool IsEnableAutoLogin = true;


        /// <summary>
        /// 错误提示消息
        /// </summary>
        public string ErrorTip { set { lblErrorr.Text = value; } }
        

        public FrmLogin():this(true)
        {
        }
        public FrmLogin(bool IsEnableAutoLogin)
        {
            InitializeComponent();
            this.Load += FrmLogin_Load;
            this.IsEnableAutoLogin = IsEnableAutoLogin;
        }
        #region 事件

        #region 窗体加载事件
        private void FrmLogin_Load(object sender, EventArgs e)
        {
            txtAccount.Text = ConfigModel.UserName;
            txtPassword.Text = ConfigModel.Password;
            txtAutoLogin.Checked = ConfigModel.AutoLogin;
            txtAccount.Focus();
            if (IsEnableAutoLogin && ConfigModel.AutoLogin)
            {
                logBLL.Debug("自动登录已开启...");
                Login();
            }
        }
        #endregion
        #region 取消按钮点击事件
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
        #region 登录按钮点击事件
        private void btnLogin_Click(object sender, EventArgs e)
        {
            Login();
        }
        #endregion

        #endregion
        private void Login()
        {
            string account = txtAccount.Text;
            string password = txtPassword.Text;
            bool autoLogin = txtAutoLogin.Checked;
            User user = new User()
            {
                Account = account,
                Password = password
            };

            string tip = "";
            if (string.IsNullOrEmpty(password))
            {
                tip = "密码不能为空!";
                txtPassword.Focus();
            }
            if (string.IsNullOrEmpty(account))
            {
                tip = "账号不能为空!";
                txtPassword.Focus();
            }
            if (string.IsNullOrEmpty(tip))
            {
                logBLL.Debug("账号[{0}]正在登录...", user.Account);
                ShowWaiting("正在登录...");
                BaseResult<User> br = userBLL.Login(user, autoLogin);
                CloseWaiting();
                if (br.Code == Code.OK)
                {
                    DialogResult = DialogResult.OK;
                    logBLL.Info("用户[{0}]登录成功!", user.Name);
                    logBLL.Debug("用户ID:{0}", user.Id);
                    this.Close();
                }
                if (br.Code == Code.LOGOUT)
                {
                    logBLL.Info("账号[{0}]登录失败:{1}", user.Account,"用户名或密码错误!");
                    lblErrorr.Text = "用户名或密码错误";
                }
                if (br.Code == Code.NULL)
                {
                    logBLL.Error("账号[{0}]登录失败:{1}", user.Account, br.Message);
                    lblErrorr.Text = br.Message;
                }
            }
            else
            {
                lblErrorr.Text = tip;
            }
        }
    }
}